package com.hui.admin.controller;

import com.hui.admin.annotation.Log;
import com.hui.admin.common.result.Result;
import com.hui.admin.common.result.Results;
import com.hui.admin.dto.request.BookPageRequestDTO;
import com.hui.admin.dto.response.BookPageResponseDTO;
import com.hui.admin.entity.BookDO;
import com.hui.admin.service.BookService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * 图书控制层
 */
@RestController
@RequestMapping("/api/book")
@RequiredArgsConstructor
public class BookController {

    private final BookService bookService;

    /**
     * 获取图书分页列表
     */
    @GetMapping
    public Result<BookPageResponseDTO> getBookPageList(BookPageRequestDTO requestDTO) {
        BookPageResponseDTO responseDTO = bookService.getBookPageList(requestDTO);
        return Results.success(responseDTO);
    }

    /**
     * 新增图书
     */
    @PostMapping
    @Log("新增图书")
    public Result<Void> createBook(@RequestBody BookDO bookDO) {
        bookService.createBook(bookDO);
        return Results.success();
    }

    /**
     * 修改图书
     */
    @Log("修改图书")
    @PutMapping
    public Result<Void> updateBook(@RequestBody BookDO bookDO) {
        bookService.updateBook(bookDO);
        return Results.success();
    }

    /**
     * 删除图书
     */
     @DeleteMapping("/{id}")
     @Log("删除图书")
     public Result<Void> deleteBookById(@PathVariable Long id) {
          bookService.deleteBook(id);
          return Results.success();
     }

    /**
     * 批量删除图书
     */
    @Log("批量删除图书")
    @PostMapping("/deleteBatch")
    public Result<Void> deleteBooks(@RequestBody Map<String, Long[]> payload) {
        Long[] ids = payload.get("ids");
        bookService.deleteBooks(ids);
        return Results.success();
    }
}
